<?php
include_once(dirname(__FILE__) . "/../config.php");

class Task {
    private $mSummary = null;
    private $mExtra = null;
    private $mPendingTime = 0;

    public function Task($summary = array()) {
        if (empty($summary)) {
            $summary = array(
                "id" => 0,
                "parser" => 0,
                "owner" => 0,
                "target" => "",
                "title" => "",
                "comments" => "",
                "result" => "",
                "create_time" => "0",
                "result_time" => "",
                "stat" => 0,
            );
        }
        $this->mSummary = $summary;
    }

    public function id() {
        return $this->mSummary["id"];
    }

    public function parser() {
        return $this->mSummary["parser"];
    }

    public function owner() {
        return $this->mSummary["owner"];
    }

    public function target() {
        return $this->mSummary["target"];
    }

    public function targetUrl() {
        return UPLOAD_URL . $this->target();
    }

    public function title() {
        return $this->mSummary["title"];
    }

    public function comments() {
        return $this->mSummary["comments"];
    }

    public function result() {
        return $this->mSummary["result"];
    }

    public function resultUrl() {
        return RESULT_URL . $this->result();
    }

    public function create_time() {
        return (int)$this->mSummary["create_time"];
    }

    public function result_time() {
        return (int)$this->mSummary["result_time"];
    }

    public function status() {
        return $this->mSummary["stat"];
    }

    public function status_text() {
        $s = $this->status();
        if ($s == db_task::STATUS_PARSED) {
            return "OK";
        } else if ($s == db_task::STATUS_ERROR) {
            return "Error";
        } else {
            return "Pending";
        }
    }


    public function hasParsed() {
        return ($this->status() == db_task::STATUS_PARSED);
    }

    public function isOpen() {
        return ($this->status() == db_task::STATUS_OPEN);
    }

    public function isPending() {
        return ($this->status() == db_task::STATUS_PENDING);
    }

    public function setParser($v) {
        $this->mSummary["parser"] = $v;
    }

    public function setTarget($v) {
        $this->mSummary["target"] = $v;
    }

    public function setTitle($v) {
        $this->mSummary["title"] = $v;
    }

    public function setComments($v) {
        $this->mSummary["comments"] = $v;
    }

    public function setStatus($v) {
        $this->mSummary["stat"] = $v;
    }

    public function setParsed() {
        $this->setStatus(db_task::STATUS_PARSED);
    }

    public function setOpen() {
        $this->setStatus(db_task::STATUS_OPEN);
    }

    public function setPending() {
        $this->setStatus(db_task::STATUS_PENDING);
        $this->mPendingTime = time(null);
    }

    public function pendingTime() {
        return $this->mPendingTime;
    }

    public function extra() {
        return $this->mExtra;
    }
    public function setExtra($v) {
        $this->mExtra = $v;
    }

    public function save() {
        $id = $this->id();
        if ($id == 0) {
            $id = db_task::inst()->add($this->parser(), $this->owner(), $this->target(), $this->title(), $this->comments());
            if ($id !== false) {
                $this->mSummary["id"] = $id;
            }
        } else {
            // $id = db_task::inst()->modify($id);
        }
        return $id;
    }

    public function packInfo() {
        $ct = Date("Y-m-d H:i:s", $this->create_time());
        $rt = $this->result_time() > 0 ? Date("Y-m-d H:i:s", $this->result_time()) : "";
        return array(
            "id" => $this->id(),
            // "parser" => $this->parser(), 
            "owner" => $this->owner(), 
            "target" => $this->target(), 
            "title" => $this->title(), 
            "comments" => $this->comments(), 
            "result" => $this->resultUrl(), 
            "create_time" => $ct,
            "result_time" => $rt,
            "stat" => $this->status(),
            "status_text" => $this->status_text(),
        );
    }

    public function toString() {
        return json_encode($this->packInfo());
    }

    public static function create($id) {
        $summary = db_task::inst()->get($id);
        return new Task($summary);
    }

    public static function all() {
        $items = db_task::inst()->all();
        $arr = array();
        foreach ($items as $id => $summary) {
            $arr[$id] = new Task($summary);
        }
        return $arr;
    }

    public static function allParser($pid) {
        $items = db_task::inst()->allParser($pid);
        $arr = array();
        foreach ($items as $id => $summary) {
            $arr[$id] = new Task($summary);
        }
        return $arr;
    }


    public static function &cachedAll() {
        $cache = cache::instance();
        $all = $cache->load("class.task.all", null);
        if ($all === null) {
            $all = Task::all();
            $cache->save("class.task.all", $all);
        }
        return $all;
    }

    public static function remove($id) {
        return db_task::inst()->remove($id);
    }

    public static function updateResult($id, $result) {
        return db_task::inst()->update_result($id, $result);
    }

    public static function updateError($id) {
        return db_task::inst()->error($id);
    }
};

